package com.neusoft.neuiotms.dcvm.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcvm.dto.ModelDTO;
import com.neusoft.neuiotms.dcvm.dto.ModelVO;
import com.neusoft.neuiotms.dcvm.model.Model;
import com.neusoft.neuiotms.dcvm.model.ModelVersion;
import com.neusoft.neuiotms.dcvm.repository.ModelRepository;
import com.neusoft.neuiotms.dcvm.repository.ModelVersionRepository;

@Component
public class ModelConverter extends AbstractConverter<Model, ModelDTO> {

    @Autowired
    private ModelRepository modelRepository;
    @Autowired
    private ModelVersionRepository modelVersionRepository;
    @Autowired
    private ModelVersionConverter modelVersionConverter;

    @Override
    public Model toModel(ModelDTO dto) {
        Model model;
        if (dto.isNew()) {
            model = new Model();
        } else {
            model = this.modelRepository.getOne(dto.getId());
        }
        model.setHeight(dto.getHeight());
        model.setLength(dto.getLength());
        model.setWidth(dto.getWidth());
        model.setModelCode(dto.getModelCode());
        model.setModelName(dto.getModelName());
        model.setTypeId(dto.getTypeId());
        model.setPreviewFsId(dto.getImgUrl());
        model.setMapFsId(dto.getMapUrl());
        final List<ModelVersion> versions = model.getVersionList().stream().collect(Collectors.toList());
        versions.forEach(v -> {
            model.getVersionList().remove(v);
            // 如果想删除多方的数据，不管一方是否放弃维护关系，都需要多方显示删除
            this.modelVersionRepository.delete(v);
        });
        dto.getVersionList().stream().map(it -> {
            final ModelVersion version = this.modelVersionConverter.toModel(it);
            // 由于一方放弃维护关系，所以多方要显示设定一方
            version.setModel(model);
            return version;
        }).forEach(model.getVersionList()::add);
        return model;
    }

    @Override
    public ModelDTO toDTO(Model model, boolean forListView) {
        final ModelDTO dto = new ModelDTO();
        dto.setId(model.getId());
        dto.setHeight(model.getHeight());
        dto.setLength(model.getLength());
        dto.setWidth(model.getWidth());
        dto.setModelCode(model.getModelCode());
        dto.setModelName(model.getModelName());
        dto.setTypeId(model.getTypeId());
        dto.setImgUrl(model.getPreviewFsId());
        dto.setMapUrl(model.getMapFsId());
        model.getVersionList().stream().map(it -> this.modelVersionConverter.toDTO(it))
                .forEach(dto.getVersionList()::add);
        return dto;
    }

    public ModelDTO toDTO(ModelVO vo) {
        final ModelDTO dto = new ModelDTO();
        dto.setHeight(vo.getHeight());
        dto.setLength(vo.getLength());
        dto.setWidth(vo.getWidth());
        dto.setModelCode(vo.getModelCode());
        dto.setModelName(vo.getModelName());
        dto.setTypeId(vo.getTypeId());
        return dto;
    }

}
